package dev.android.yeqh.iask_sina.adapter;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import dev.android.yeqh.iask_sina.ItemDetailActivity;
import dev.android.yeqh.iask_sina.R;
import dev.android.yeqh.iask_sina.model.cidianItem;

public class SearchResultAdapter extends BaseAdapter{
	Activity context = null;
	List<cidianItem> items = null;
	int layout = 0;
	OnClickListener clickListener = new OnClickListener() {
		
		public void onClick(View view) {
			Intent intent = new Intent(context, ItemDetailActivity.class);
    		intent.putExtra("title", (String)view.getTag());
    		context.startActivity(intent);
		}
	};
	
	public SearchResultAdapter(Activity context, int layout) {
		this.context = context;
		this.layout = layout;
		this.items = new ArrayList<cidianItem>();
	}
	
	public void setItems(List<cidianItem> items) {
		for (cidianItem item : items) {
			this.items.add(item);
		}
	}
	
	public void clearItems() {
		this.items.clear();
	}

	public int getCount() {
		return items.size();
	}

	public Object getItem(int position) {
		return items.get(position);
	}

	public long getItemId(int position) {
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View view = context.getLayoutInflater().inflate(layout, null);
		TextView txtTitle = (TextView)view.findViewById(R.id.txt_title);
		TextView txtSummary = (TextView)view.findViewById(R.id.txt_summary);
		TextView txtUpdateTime = (TextView)view.findViewById(R.id.txt_updatetime);
		
		cidianItem item = items.get(position);
		
		txtTitle.setText(item.getTitle());
		txtSummary.setText(item.getSummary());
		txtUpdateTime.setText(item.getDate());
		view.setTag(item.getTitle());
		view.setOnClickListener(clickListener);
		
		return view;
	}

}
